<!--
 * @description  :窝点信息维护
 * @descriptionDetail:窝点信息维护
 * @copyright    : 浙江烟草
 * @author       : dw
 * @create       : 2022-10-09 22:21:21
-->
<template>
  <view>
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="nolicence-search-lay" :style="{'top': isHideNav ? 0 : '44px'}">
      <u-search
        shape="round"
        :clearabled="true"
        :showAction="true"
        actionText="确定"
        :animation="true"
        placeholder="窝点名称/窝点地址"
        v-model="searchKey"
        @custom="toSearchList"
      ></u-search>
    </view>
    <view class="content u-page">
      <me-table :i="0" :index="0" :top="112" :load-data="loadData" :height="getListViewHeight()" :init-load="false" ref="mescrollItem0">
        <view slot-scope="d" class="list-item-root">
          <u-swipe-action
            :show="d.data.show"
            :index="d.data.uuid"
            :disabled="d.data.swiperDisabled"
            @click="swiperClick"
            @open="swiperOpen"
            :options="d.data.swiperOptions"
          >
            <view class="list-lay">
              <list-item-hideout :itemInfo="d.data"></list-item-hideout>
            </view>
          </u-swipe-action>
        </view>
      </me-table>
    </view>
    <view class="submit-btns btn-other-cls">
      <u-button class="u-btn" type="primary" @click="addMenu">
        添加
      </u-button>
    </view>
  </view>
</template>

<script>
import intelligenceservice from "@/service/intelligence/intelligence.service";
import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
import meTable from "@/components/me-table.vue";
import ListItemHideout from "./components/listItemHideout.vue";
import {CONTROL_STATUS,} from "@/service/intelligence/mapping/enum.js";
export default {
  components: {
    meTable,
    ListItemHideout,
  },
  mixins: [MescrollMoreMixin,],
  data() {
    return {
      searchKey: "",
      user: this.$storage.get(this.$rp.common.USER),
      defaultOrg: undefined,
      orgName: "",
      orgUuid: "",
    };
  },
  methods: {
    getListViewHeight() {
      let p = this.$u.sys().windowHeight - 44 - 48 - uni.upx2px(10) - this.$u.sys().statusBarHeight + "px";
      // #ifdef H5-DINGDING || H5-WECHAT
      p = this.$u.sys().windowHeight - uni.upx2px(10) - 48 - this.$u.sys().statusBarHeight + "px";
      // #endif
      return p;
    },
    toSearchList(e) {
      this.getMescroll(0).triggerDownScroll();
    },
    addMenu() {
      this.$u.route({
        url: "/pages/intelligence/intelligenceFilesAdd/hideout",
        params: {
          from: "add",
          orgName: this.orgName,
          orgUuid: this.orgUuid,
        },
      });
    },

    loadData(page) {
      return new Promise((resolve, reject) => {
        let params = {
          page: page.num,
          limit: page.size,
          paras: {
            fuzzyQuery: this.searchKey,
          },
        };
        if (this.defaultOrg) {
          params.paras.orgUuid = this.defaultOrg.id;
          intelligenceservice
            .denPage(params)
            .then((res) => {
              if (res.success) {
                res.data.forEach((itm) => {
                  this.getItemCanEdit(itm);
                  itm.show = false;
                  itm.swiperDisabled = false;
                });
                resolve({
                  count: res.count,
                  data: res.data,
                });
              } else {
                this.$u.toast(res.msg);
                resolve({
                  count: 0,
                  data: [],
                });
              }
            })
            .catch((e) => {
              this.$u.toast("请求失败:" + e);
              resolve({
                count: 0,
                data: [],
              });
            });
        } else {
          this.getDefaultOrg("", "zm-all", (r) => {
            if (r) {
              this.defaultOrg = r;
              this.orgName = r.name;
              this.orgUuid = r.id;
              params.paras.orgUuid = this.defaultOrg.id;
            } else {
              params.paras.orgUuid = "";
            }
            intelligenceservice
              .denPage(params)
              .then((res) => {
                if (res.success) {
                  res.data.forEach((itm) => {
                    this.getItemCanEdit(itm);
                    itm.show = false;
                    itm.swiperDisabled = false;
                  });
                  resolve({
                    count: res.count,
                    data: res.data,
                  });
                } else {
                  this.$u.toast(res.msg);
                  resolve({
                    count: 0,
                    data: [],
                  });
                }
              })
              .catch((e) => {
                this.$u.toast("请求失败:" + e);
                resolve({
                  count: 0,
                  data: [],
                });
              });
          });
        }
      });
    },
    getItemCanEdit(item){
      let disabled = true;
      let swiperOptions = [];
      if(item.ableOperate == 1){
        disabled = false;
        swiperOptions.push({
          text: '编辑',
          action:"edit",
          style: {
            backgroundColor: this.$theme.u_type_primary,
          },
        });
      }
      if(item.isActive == CONTROL_STATUS.DEACTIVATE){
        swiperOptions.push({
          text: '停用',
          action:"deactivate",
          style: {
            backgroundColor: this.$theme.u_type_error,
          },
        });
      } else {
        swiperOptions.push({
          text: '启用',
          action:"enable",
          style: {
            backgroundColor: this.$theme.u_type_success,
          },
        });
      }
      item.swiperOptions = swiperOptions;
      item.swiperDisabled = disabled;
    },
    // index1是数据索引 index是导航索引
    swiperClick(index1, index) {
      let datas = this.$refs.mescrollItem0.listDatas;
      let cur = -1;
      for (let i = 0; i < datas.length; i++) {
        if (datas[i].uuid == index1) {
          cur = i;
          break;
        }
      }
      if (cur == -1) {
        this.$u.toast("选中数据无效");
        return;
      }
      if (index == 0) {
        // 修改
        if (datas[cur].ableOperate == 1) {
          let params = datas[cur];
          this.setRouteParam(params);
          this.$u.route({
            url: "/pages/intelligence/intelligenceFilesAdd/hideout",
            params: {
              from: "edit",
            },
          });
          this.$refs.mescrollItem0.listDatas.map((val, idx) => {
            this.$refs.mescrollItem0.listDatas[idx].show = false;
          });
          return;
        } else {
          this.swiperBtn(datas[cur].uuid,datas[cur].isActive);
        }
      } else if (index == 1) {
        this.swiperBtn(datas[cur].uuid,datas[cur].isActive);
      }
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = false;
      });
    },
    swiperBtn (id,isActive) {
      if (isActive == 1) {
        this.$modal.confirm("确定要停用吗？", () => {
          this.$modal.showLoading("停用中...");
          let params = { "uuid": id, "type": "1", };
          intelligenceservice.changeActive(params).then(res => {
            if(res.success) {
              this.$u.toast("停用成功");
              this.getMescroll(0).triggerDownScroll();
            } else {
              this.$message.error(res.msg);
            }
          }).catch(err => {
            this.$modal.hideLoading();
            this.$u.toast("停用失败" + err);
          })
        });
      } else {
        this.$modal.confirm("确定要启用吗？", () => {
          this.$modal.showLoading("启用中...");
          let params = {"uuid":id,"type":"1",};
          intelligenceservice.changeActive(params).then(res => {
            if(res.success) {
              this.$u.toast("启用成功");
              this.getMescroll(0).triggerDownScroll();
            } else {
              this.$message.error(res.msg);
            }
          }).catch(err => {
            this.$modal.hideLoading();
            this.$u.toast("启用失败" + err);
          })
        });
      }
    },
    swiperOpen(index) {
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = index == val.uuid;
      });
    },
    updateBackState(param) {
      this.getMescroll(0).triggerDownScroll();
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/style/common/rightMenu.scss";

.content {
  background-color: #f3f4f5;
  min-height: calc(100vh - 46px);
}
.nolicence-search-lay {
  background-color: #fff;
  position: fixed;
  // #ifndef H5-DINGDING
  top: 44px;
  // #endif
  // #ifdef H5-DINGDING
  top: 0px;
  // #endif
  z-index: 999;
  width: 100%;
  overflow: hidden;
  padding: $cm-search-padding-row $cm-search-padding-column;
}
.key-search {
  width: 100%;
  margin: auto;
  padding-left: 2.5%;
  padding-right: 2.5%;
  line-height: 48px;
}

.pop-content-lay {
  padding: 20rpx;
  display: flex;
  height: 100%;
  flex-direction: column;
  justify-content: center;
}

.grid-text {
  font-size: 28rpx;
  margin-top: 4rpx;
  color: $u-type-info;
}

.list-item-root {
  margin: 10px;
  border-radius: 15px;
  overflow: hidden;
}
.list-lay {
  margin: 10px;
}
</style>
